<?php
/**
 * mSanpham 
 * - Define class mSanpham
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
include_once(DOCUMENT_ROOT . "/lib/core/BaseProcess.class.php");
class MSanpham extends BaseProcess {
	public function __construct() {
		parent::__construct("sanpham");
		$this->primaryKeyName = "id";
	}
	
	public function updateSoLuong($id, $sl=1) {
		if (is_numeric($id)) {
			$attr = array('id' => $id);
			$field = array('id', 'soluong');
			$data = $this->getOneByAttr($attr, $field);
			if (is_array($data) && !empty($data)) {
				$this->setAttribute('soluong', ($data['soluong'] - $sl > 0) ? ($data['soluong'] - $sl) : '0');
				parent::update(array('id' => $data['id']));
			}
		}
	}
	
	public function checkTonTai($danhmuc_id = '', $iddm = '') {
		$sql = "SELECT count(*) as count_id FROM " . $this->tableName . " WHERE danhmuc_id = '" . $danhmuc_id . "' AND iddm ='" . $iddm . "'" .
			   " AND iddmc IN (SELECT id FROM sp_sub_category WHERE danhmuc_id=" . $danhmuc_id . " AND iddm=".$iddm.")";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		return ($rs && $rs->fields['count_id'] > 0) ? false : true;
	}
	
	public function getListAll($danhmuc_id = '', $iddm = '', $subiddm = '', $flag = false) {
		$sql = "SELECT id, danhmuc_id, iddm, iddmc, code, name, gia1, check_km, gia2, soluong FROM " . $this->tableName. " WHERE 1=1";
		if ($danhmuc_id != '') {
			$sql .= " AND danhmuc_id = '" . $danhmuc_id . "'";
		}
		if ($iddm != '') {
			$sql .= " AND iddm='" . $iddm . "'";
		}
		if ($subiddm != '' || $flag) {
			$sql .= " AND iddmc='" . $subiddm . "'";
		}
		$sql .= " ORDER BY seq DESC";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
        $data = array();
        if ($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) {
                $data[] = $item;
            }
        }
        return $data;
	}
	
	public function sanPhamHome($atrr, $field, $order = 'seq', $styleOrder = 'DESC') {
		$sql = "SELECT " . $field;
		$sql .= " FROM sp_category as d, sp_sub_category as c, sanpham as s WHERE d.id=s.iddm AND c.id=s.iddmc";
		foreach ($atrr as $key=>$value) {
        	if (is_array($value)) {
        		$value['value'] = "'" . mysql_real_escape_string($value['value']) . "'";
	            $sql .= " AND " . $key . $value['math'] . $value['value'];	
        	} else {
        		if ($value !== null) {
	                $value = "'" . mysql_real_escape_string($value) . "'";
	                $sql .= " AND $key=$value";
	            }else {
	                $sql .= " AND $key IS NULL";
	            }	
        	}
        }
        $sql .= " ORDER BY " . $order . " " . $styleOrder;
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
        $result = array();
        if($rs) {
            while (! $rs->EOF && $item = $rs->FetchRow()) { 
                $result[] = $item;
            }
        }
        return $result;
	}
	
	public function getDanhMucID($idSP) {
		$sql = "SELECT danhmuc_id FROM " . $this->tableName . " WHERE id = '" . mysql_real_escape_string($idSP) . "'";
		$rs = $this->execute($sql, ADODB_FETCH_ASSOC);
		return ($rs) ? $rs->fields['danhmuc_id'] : '';
	}

}
?>